<?php
require ("userCentreConfig.php");
class userCentreDao
{
    private $m_mongoclt;
    private $m_databbase;
    private $m_collection;

    public function __construct($strHost, $intPort, $strDBname, $strTableName)
    {
        $this->m_mongoclt   = new Mongo($strHost.":".$intPort);
        $this->m_databbase  = $this->m_mongoclt->$strDBname;
        $this->m_collection = $this->m_databbase->$strTableName;
    }

    public function __destruct()
    {
        $this->m_mongoclt->close();
    }

    public function getUserInfo($intUserID)
    {
        return $this->m_collection->findOne(array( "_id" => $intUserID ));
    }

    public function getUserInfoList($arrUserID)
    {
        $cursor = $this->m_collection->find( array ('_id' => array('$in' => $arrUserID) ) );
        return iterator_to_array($cursor);
    }

    public function setUserInfo($intUserID, $arrUserInfo)
    {
        $arrUserCentreConfig = new userCentreConfig();
        $arrFieldName = $arrUserCentreConfig->getUserCentreFieldName();
        $arrUpdate = array();
        foreach ($arrUserInfo as $key => $value)
        {
            if (array_key_exists($key, $arrFieldName) && $this->validType($arrFieldName[$key], $value))
            {
                $arrUpdate[$key] = $value;
            }
            else 
            {
                echo "fieldname: ".$key." is NOT in FieldLis OR type ERROR.\n";
            }
        }
        return $this->m_collection->update(array("_id" => $intUserID), array('$set' => $arrUpdate), true);
    }

    private function validType($intTypeID, $value)
    {
        switch ($intTypeID["TYPE"])
        {
            case T_INT_FIELD:
                return is_int($value);
                break;
            case T_STR_FIELD:
                return is_string($value);
                break;
            case T_ARR_FIELD:
                return is_array($value);
                break;
            default:
                break;
        }
    }
};
?>
